﻿// <copyright file="NomineeList.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-12-08</date>
// <summary>Nominee List class definition</summary>

namespace NYTimes
{
    using System;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;
    using NYTimes.Core;
    using NYTimes.Data;

    /// <summary>
    /// New York Times API to get lists of presidential nominations for civilian positions.
    /// </summary>
    public class NomineeList : CongressApi
    {
        /// <summary>
        /// Initializes a new instance of the NomineeList class
        /// </summary>
        public NomineeList()
        {
            this.Nominations = new ObservableCollection<Nomination>();
        }

        /// <summary>
        /// Gets or sets the congress number
        /// </summary>
        public int CongressNumber { get; set; }

        /// <summary>
        /// Gets or sets the nomination category
        /// </summary>
        public NominationCategory Category { get; set; }

        /// <summary>
        /// Gets the nominations
        /// </summary>
        public ObservableCollection<Nomination> Nominations { get; private set; }

        /// <summary>
        /// returns true if the congress number is greater than 0
        /// </summary>
        /// <returns>true if the congress number is greater than 0</returns>
        public override bool ShouldFetchResults()
        {
            if (this.CongressNumber <= 0)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Clear the result set
        /// </summary>
        public override void ClearResults()
        {
            this.Nominations.Clear();
        }

        /// <summary>
        /// Generate the Url for the service
        /// </summary>
        /// <param name="baseUri">the base URL</param>
        /// <returns>the full URL</returns>
        public override Uri GenerateUrl(string baseUri)
        {
            var uriString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}/nominees/{1}.xml?api-key={2}",
                this.CongressNumber,
                this.Category,
                this.ApiKey);

            return new Uri(uriString, UriKind.Absolute);
        }

        /// <summary>
        /// Parse the nominee list
        /// </summary>
        /// <param name="results">the results element</param>
        public override void OnParseResults(System.Xml.Linq.XElement results)
        {
            if (results == null)
            {
                throw new ArgumentNullException("results");
            }

            var nominationResults = results.Element("nominations").Elements("nomination");

            var nominations = from nomination in nominationResults
                              select new Nomination()
                              {
                                  CommitteeUri = new Uri(nomination.Element("uri").Value, UriKind.Relative),
                                  DateReceived = DateTime.Parse(nomination.Element("date_received").Value, CultureInfo.InvariantCulture),
                                  Description = nomination.Element("description").Value,
                                  Id = nomination.Element("id").Value,
                                  LatestAction = nomination.Element("latest_action").Value,
                                  LatestActionDate = DateTime.Parse(nomination.Element("latest_action_date").Value, CultureInfo.InvariantCulture),
                                  NomineeState = nomination.Element("nominee_state").Value,
                                  Status = (Data.NominationCategory)Enum.Parse(typeof(Data.NominationCategory), nomination.Element("status").Value, true),
                                  Uri = new Uri(nomination.Element("uri").Value, UriKind.Relative)
                              };

            foreach (var nomination in nominations)
            {
                this.Nominations.Add(nomination);
            }
        }
    }
}
